package com.bj58.groupbuy.action.charts;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.bj58.groupbuy.action.db.DBConnectionPool;
import com.bj58.groupbuy.action.db.JdbcUitl;
import com.bj58.wf.log.Log;
import com.bj58.wf.log.LogFactory;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class ChartSearchDao {
	private static Log log = LogFactory.getLog(ChartSearchDao.class);
	
		
	/**曲线图通用查询   按列组织数据
	 * @param Sql
	 * @param colTypes
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public static  Map<String, Collection<String>> searchLineData(String Sql,List<String> colNames,int type){
		Multimap<String, String> resultMap = ArrayListMultimap.create();
		Connection con = DBConnectionPool.getConnection(type);
		if(null == con){
			for(int i = 0 ; i < 2 ; i++){
				log.info("try again "+i);
				con = DBConnectionPool.getConnection(type);
				if(null != con){
					break;
				}
			}
		}
		Statement st = null;
		ResultSet rs = null;
		try {
			log.info("ir  con  is  " + con);
			st = con.createStatement();
			rs = st.executeQuery(Sql);
			int colCount = colNames.size();
			while(rs.next()){
				for(int i = 0; i< colCount; i++){
					String label = colNames.get(i);
					String  value =String.valueOf(rs.getObject(i+1));
					if(null == value || "".equals(value) || "null".equals(value)){
						value = "0";
					}
					resultMap.put(label, value);
				}
			}
			return resultMap.asMap();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUitl.closeResultSet(rs); JdbcUitl.closeStatement(st); JdbcUitl.closeConnection(con);
		}
		return null;
	}
	
}
